<div class="overflow-x-auto">
  <table
    class="gf-table w-100"
    mat-table
    matSort
    matSortActive="allocationInPercentage"
    matSortDirection="desc"
    [dataSource]="dataSource"
  >
    <ng-container matColumnDef="icon" sticky>
      <th *matHeaderCellDef class="px-1" mat-header-cell></th>
      <td *matCellDef="let element" class="px-1 text-center" mat-cell>
        <gf-entity-logo
          [dataSource]="element.dataSource"
          [symbol]="element.symbol"
          [tooltip]="element.name"
        />
      </td>
    </ng-container>

    <ng-container matColumnDef="nameWithSymbol">
      <th
        *matHeaderCellDef
        class="px-1"
        mat-header-cell
        mat-sort-header="symbol"
      >
        <ng-container i18n>Name</ng-container>
      </th>
      <td *matCellDef="let element" class="line-height-1 px-1" mat-cell>
        <div class="text-truncate">
          {{ element.name }}
          @if (element.name === element.symbol) {
            <span>({{ element.assetSubClassLabel }})</span>
          }
        </div>
        <div>
          <small class="text-muted">{{ element.symbol }}</small>
        </div>
      </td>
    </ng-container>

    <ng-container matColumnDef="dateOfFirstActivity">
      <th
        *matHeaderCellDef
        class="d-none d-lg-table-cell justify-content-end px-1"
        mat-header-cell
        mat-sort-header
      >
        <ng-container i18n>First Activity</ng-container>
      </th>
      <td
        *matCellDef="let element"
        class="d-none d-lg-table-cell px-1"
        mat-cell
      >
        <div class="d-flex justify-content-end">
          <gf-value
            [isDate]="element.dateOfFirstActivity ? true : false"
            [locale]="locale"
            [value]="element.dateOfFirstActivity ?? ''"
          />
        </div>
      </td>
    </ng-container>

    <ng-container matColumnDef="quantity">
      <th
        *matHeaderCellDef
        class="d-none d-lg-table-cell justify-content-end px-1"
        mat-header-cell
        mat-sort-header
      >
        <ng-container i18n>Quantity</ng-container>
      </th>
      <td
        *matCellDef="let element"
        class="d-none d-lg-table-cell px-1"
        mat-cell
      >
        <div class="d-flex justify-content-end">
          <gf-value
            [isCurrency]="true"
            [locale]="locale"
            [value]="isLoading ? undefined : element.quantity"
          />
        </div>
      </td>
    </ng-container>

    <ng-container matColumnDef="valueInBaseCurrency">
      <th
        *matHeaderCellDef
        class="d-none d-lg-table-cell justify-content-end px-1"
        mat-header-cell
        mat-sort-header
      >
        <ng-container i18n>Value</ng-container>
      </th>
      <td
        *matCellDef="let element"
        class="d-none d-lg-table-cell px-1"
        mat-cell
      >
        <div class="d-flex justify-content-end">
          <gf-value
            [isCurrency]="true"
            [locale]="locale"
            [value]="isLoading ? undefined : element.valueInBaseCurrency"
          />
        </div>
      </td>
    </ng-container>

    <ng-container matColumnDef="allocationInPercentage">
      <th
        *matHeaderCellDef
        class="justify-content-end px-1"
        mat-header-cell
        mat-sort-header
      >
        <span class="d-none d-sm-block" i18n>Allocation</span>
        <span class="d-block d-sm-none" title="Allocation">%</span>
      </th>
      <td *matCellDef="let element" class="px-1" mat-cell>
        <div class="d-flex justify-content-end">
          <gf-value
            [isPercent]="true"
            [locale]="locale"
            [value]="isLoading ? undefined : element.allocationInPercentage"
          />
        </div>
      </td>
    </ng-container>

    <ng-container matColumnDef="performance">
      <th
        *matHeaderCellDef
        class="justify-content-end px-1"
        mat-header-cell
        mat-sort-header="netPerformanceWithCurrencyEffect"
      >
        <ng-container i18n>Change</ng-container>
      </th>
      <td *matCellDef="let element" class="px-1" mat-cell>
        <div class="d-flex justify-content-end">
          <gf-value
            [colorizeSign]="true"
            [isCurrency]="true"
            [locale]="locale"
            [value]="
              isLoading ? undefined : element.netPerformanceWithCurrencyEffect
            "
          />
        </div>
      </td>
    </ng-container>

    <ng-container matColumnDef="performanceInPercentage" stickyEnd>
      <th
        *matHeaderCellDef
        class="justify-content-end px-1"
        mat-header-cell
        mat-sort-header="netPerformancePercentWithCurrencyEffect"
      >
        <span class="d-none d-sm-block" i18n>Performance</span>
        <span class="d-block d-sm-none" title="Performance">±</span>
      </th>
      <td *matCellDef="let element" class="px-1" mat-cell>
        <div class="d-flex justify-content-end">
          <gf-value
            [colorizeSign]="true"
            [isPercent]="true"
            [locale]="locale"
            [value]="
              isLoading
                ? undefined
                : element.netPerformancePercentWithCurrencyEffect
            "
          />
        </div>
      </td>
    </ng-container>

    <tr *matHeaderRowDef="displayedColumns" mat-header-row></tr>
    <tr
      *matRowDef="let row; columns: displayedColumns"
      mat-row
      [ngClass]="{
        'cursor-pointer':
          hasPermissionToOpenDetails &&
          !ignoreAssetSubClasses.includes(row.assetSubClass)
      }"
      (click)="
        !ignoreAssetSubClasses.includes(row.assetSubClass) &&
          onOpenHoldingDialog({
            dataSource: row.dataSource,
            symbol: row.symbol
          })
      "
    ></tr>
  </table>
</div>

<mat-paginator class="d-none" [pageSize]="pageSize" />

@if (isLoading) {
  <ngx-skeleton-loader
    animation="pulse"
    class="px-4 py-3"
    [theme]="{
      height: '1.5rem',
      width: '100%'
    }"
  />
}

@if (dataSource.data.length > pageSize && !isLoading) {
  <div class="my-3 text-center">
    <button mat-stroked-button (click)="onShowAllHoldings()">
      <ng-container i18n>Show all</ng-container>
    </button>
  </div>
}
